﻿Public Class ClsTWorkPeriodDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_T_WORK_PERIOD)
    End Sub

    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    ''' <summary>
    ''' 删除指定期间的过账数据，这是反过账操作
    ''' </summary>
    ''' <param name="wp"></param>
    ''' <returns></returns>
    Public Function DeleteWorkPeriod(ByVal wp As String) As Integer

        Dim tmpRetInt As Integer = 0

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpParas(0) As OleDb.OleDbParameter
            Dim tmpSql As String = "Delete * from [" & Me.AppTableName & "] "
            tmpSql &= " Where [WorkPeriod]=@WorkPeriod "

            tmpParas(0) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)
            tmpParas(0).Value = ConvertStringDbNull(wp)

            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetInt = tmpDb.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTWorkPeriodDbAccess - DeleteWorkPeriod"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetInt

    End Function

    ''' <summary>
    ''' 指定期间进行结账标识处理
    ''' </summary>
    ''' <param name="wp"></param>
    ''' <param name="ifSettle"></param>
    ''' <returns></returns>
    Public Function SettleWorkPeriod(ByVal wp As String, ByVal ifSettle As Boolean) As Integer

        Dim tmpRetInt As Integer = 0

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpParas(1) As OleDb.OleDbParameter
            Dim tmpSql As String = "Update [" & Me.AppTableName & "] set "
            tmpSql &= "[Settled]=@Settled "
            tmpSql &= " Where [WorkPeriod]=@WorkPeriod "

            tmpParas(0) = New OleDb.OleDbParameter("@Settled", OleDb.OleDbType.Boolean)
            tmpParas(1) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)

            tmpParas(0).Value = ifSettle
            tmpParas(1).Value = ConvertStringDbNull(wp)
            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetInt = tmpDb.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - InsertAccount"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetInt

    End Function

    ''' <summary>
    ''' 更新会计期间表数据
    ''' </summary>
    ''' <param name="tmpCls"></param>
    ''' <returns></returns>
    Public Function UpdateWorkPeriod(ByVal tmpCls As ClsTWorkPeriod) As Integer

        Dim tmpRetInt As Integer = 0

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpParas(4) As OleDb.OleDbParameter
            Dim tmpSql As String = "Update [" & Me.AppTableName & "] set "
            tmpSql &= "[WorkPeriod]=@WorkPeriod,[VoucherCount]=@VoucherCount,[Posted]=@Posted,[Settled]=@Settled "
            tmpSql &= " Where [WorkPeriodId]=@WorkPeriodId "

            tmpParas(0) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)
            tmpParas(1) = New OleDb.OleDbParameter("@VoucherCount", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@Posted", OleDb.OleDbType.Boolean)
            tmpParas(3) = New OleDb.OleDbParameter("@Settled", OleDb.OleDbType.Boolean)
            tmpParas(4) = New OleDb.OleDbParameter("@WorkPeriodId", OleDb.OleDbType.Integer)

            tmpParas(0).Value = ConvertStringDbNull(tmpCls.WorkPeriod)
            tmpParas(1).Value = tmpCls.VoucherCount
            tmpParas(2).Value = tmpCls.Posted
            tmpParas(3).Value = tmpCls.Settled
            tmpParas(4).Value = tmpCls.WorkPeriodId
            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetInt = tmpDb.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - InsertAccount"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetInt

    End Function

    ''' <summary>
    ''' 返回指定年度的全部期间数据
    ''' </summary>
    ''' <param name="year"></param>
    ''' <returns></returns>
    Public Function GetWorkPeriodByYear(ByVal year As String) As List(Of ClsTWorkPeriod)

        Dim tmpRetList As List(Of ClsTWorkPeriod) = New List(Of ClsTWorkPeriod)()

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()

            Dim tmpParas(0) As OleDb.OleDbParameter
            Dim tmpSql As String = "Select * from  [" & Me.AppTableName & "] "
            tmpSql &= " Where [WorkPeriod] like @WorkPeriod & '%' "

            tmpParas(0) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)
            tmpParas(0).Value = year

            '打开数据库
            tmpDb.DbOpen()

            '执行
            tmpRetList = TryCast(ConvertRecordToClass(tmpDb.ExecuteSqlReturnDataSet(tmpSql, tmpParas)), List(Of ClsTWorkPeriod))

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - GetAccountByCode"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetList

    End Function

    ''' <summary>
    ''' 返回指定期间的会计期间对象
    ''' </summary>
    ''' <param name="wp"></param>
    ''' <returns></returns>
    Public Function GetWorkPeriod(ByVal wp As String) As ClsTWorkPeriod

        Dim tmpRetCls As ClsTWorkPeriod = Nothing

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()

            Dim tmpParas(0) As OleDb.OleDbParameter
            Dim tmpSql As String = "Select * from  [" & Me.AppTableName & "] "
            tmpSql &= " Where [WorkPeriod]=@WorkPeriod "

            tmpParas(0) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)
            tmpParas(0).Value = wp

            '打开数据库
            tmpDb.DbOpen()

            '执行
            Dim tmpList As List(Of ClsTWorkPeriod) = New List(Of ClsTWorkPeriod)()
            tmpList = TryCast(ConvertRecordToClass(tmpDb.ExecuteSqlReturnDataSet(tmpSql, tmpParas)), List(Of ClsTWorkPeriod))
            If tmpList.Count > 0 Then
                tmpRetCls = tmpList.Item(0)
            End If

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - GetAccountByCode"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetCls

    End Function

    ''' <summary>
    ''' 插入会计期间
    ''' </summary>
    ''' <param name="tmpCls"></param>
    ''' <returns></returns>
    Public Function InsertWorkPeriod(ByVal tmpCls As ClsTWorkPeriod) As Integer

        Dim tmpRetInt As Integer = 0

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpParas(3) As OleDb.OleDbParameter
            Dim tmpSql As String = "Insert Into [" & Me.AppTableName & "] ("
            tmpSql &= "[WorkPeriod],[VoucherCount],[Posted],[Settled] "
            tmpSql &= ") values("
            tmpSql &= "@WorkPeriod,@VoucherCount,@Posted,@Settled"
            tmpSql &= ") "

            tmpParas(0) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)
            tmpParas(1) = New OleDb.OleDbParameter("@VoucherCount", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@Posted", OleDb.OleDbType.Boolean)
            tmpParas(3) = New OleDb.OleDbParameter("@Settled", OleDb.OleDbType.Boolean)

            tmpParas(0).Value = ConvertStringDbNull(tmpCls.WorkPeriod)
            tmpParas(1).Value = tmpCls.VoucherCount
            tmpParas(2).Value = tmpCls.Posted
            tmpParas(3).Value = tmpCls.Settled

            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetInt = tmpDb.InsertData(tmpSql, tmpParas, False)

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - InsertAccount"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetInt

    End Function

    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object

        If reader Is Nothing Then
            Return Nothing
        End If

        Dim tmpList As List(Of ClsTWorkPeriod) = New List(Of ClsTWorkPeriod)

        Try

            Dim tmpCls As ClsTWorkPeriod

            While reader.Read()

                tmpCls = New ClsTWorkPeriod()

                With tmpCls
                    .WorkPeriodId = ConvertDbNullInteger(reader(0))
                    .WorkPeriod = ConvertDbNullString(reader(1))
                    .VoucherCount = ConvertDbNullInteger(reader(2))
                    .Posted = reader.GetBoolean(3)
                    .Settled = reader.GetBoolean(4)
                End With

                tmpList.Add(tmpCls)

            End While



        Catch ex As Exception

            mErrorNumber = -1001
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - ConvertRecordToClass"

        End Try

        Return tmpList

    End Function
End Class
